package com.jack.netty.udp.server;


import com.jack.netty.log.LoggerManager;
import com.jack.util.bytes.ByteUtil;
import com.jack.util.hex.HexUtil;

/**
 * Created by jack on 16/3/3.
 * 只是提供测试样板,不做任何消息处理
 */
public class SampleUdpServerListener implements UdpServerListener {

    private String HEX_START = "5137";
    private String HEX_END = "13";
    private int LENGTH_MOVING = 46;

    @Override
    public void errorInfo(String error) {

    }

    @Override
    public void messageReceived(String fromIp, int port, String msg) {

    }

    @Override
    public void messageReceived(String fromIp, int port, byte[] msg) {
        String hex = HexUtil.ByteArrToHex(msg);

        if(msg.length == LENGTH_MOVING && hex.startsWith(HEX_START) && hex.endsWith(HEX_END)){
            LoggerManager.getInstance().getiLogger().info(getClass(),"byte to hex:"+ hex);
            byte[] head = ByteUtil.subBytesStartEnd(msg,0,2); //new byte[]{msg[0],msg[1]};
            LoggerManager.getInstance().getiLogger().info(getClass(),"head:"+HexUtil.ByteArrToHex(head));

            byte[] bodyLength = ByteUtil.subBytesStartEnd(msg,2,6); //new byte[]{msg[2],msg[3],msg[4],msg[5]};
            int length = ByteUtil.lBytesToInt(bodyLength);
            LoggerManager.getInstance().getiLogger().info(getClass(),"length:"+length);

            byte[] commandBytes = ByteUtil.subBytesStartEnd(msg,6,8); //new byte[]{msg[6],msg[7]};
            String command = HexUtil.ByteArrToHex(commandBytes);
            LoggerManager.getInstance().getiLogger().info(getClass(),"command:"+command);

            byte[] idBytes = ByteUtil.subBytesStartEnd(msg,8,40);
            String id = new String(idBytes).trim();
            LoggerManager.getInstance().getiLogger().info(getClass(),"id:"+id);


        }
    }

    @Override
    public boolean isByteMsg() {
        return true;
    }
}
